//app.js
App({
  onLaunch() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var dataform = {}
    var ipaddress = 'http://localhost:8080/wxchat/superadmin/'
    // var ipaddress = 'http://k8x65m.natappfree.cc/wxchat/superadmin/'

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var that = this;
        if (res.code) {  //wx.login获取code。
          //发起网络请求
          wx.request({
            url: ipaddress + 'getOpenId',
            data: {
              code: res.code    //将code发送到后台服务器。
            },
            success(res) {
              console.log('wx.login')
              console.log(res)
              that.globalData.userid = res.data.openid
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
    //TODO:此处进行后台请求，将用户信息存储服务端
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo,
                dataform = {
                  avatarUrl: res.userInfo.avatarUrl,
                  city: res.userInfo.city,
                  country: res.userInfo.country,
                  gender: res.userInfo.gender,
                  lang: res.userInfo.language,
                  nickName: res.userInfo.nickName,
                  province: res.userInfo.province,
                  userId: this.globalData.userid
                }
              console.log(res)
              var that = this
              wx.request({
                url: ipaddress + 'registerUserFirst',
                method: 'POST',
                data: JSON.stringify(dataform),
                header: {
                  'Content-Type': 'application/json'
                },
                success: function (res) {
                  var result = res.data.success;
                  var toastText = '创建成功';
                  if (!result) {
                    toastText = '操作失败' + res.data.errMsg;
                  }
                  console.log('userState'+res.data.user.userState)
                  //页面初始化 options为页面跳转所带来的参数
                  that.state = res.data.user.userState
                  that.isAdmin = res.data.user.isAdmin
                  that.userId = res.data.user.userId
                  that.userid = res.data.user.id
                  console.log(res)
                }
              })

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },

  convertHtmlToText: function convertHtmlToText(inputText) {
    var returnText = "" + inputText;
    returnText = returnText.replace(/<\/div>/ig, '\r\n');
    returnText = returnText.replace(/<\/li>/ig, '\r\n');
    returnText = returnText.replace(/<li>/ig, '  *  ');
    returnText = returnText.replace(/<\/ul>/ig, '\r\n');
    //-- remove BR tags and replace them with line break
    returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText = returnText.replace(/<p.*?>/gi, "\r\n");
    returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText = returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g, '');

    //-- get rid of html-encoded characters:
    returnText = returnText.replace(/ /gi, " ");
    returnText = returnText.replace(/&/gi, "&");
    returnText = returnText.replace(/"/gi, '"');
    returnText = returnText.replace(/</gi, '<');
    returnText = returnText.replace(/>/gi, '>');

    return returnText;
  },

  globalData: {
    userInfo: null,
    userid: ''
  },
  state: 0,
  isAdmin:0,
  userId:'',
  userid:'',
  ipaddress:'http://localhost:8080/wxchat/superadmin/'
  // ipaddress:'http://k8x65m.natappfree.cc/wxchat/superadmin/'
})